import torch
import torch.nn as nn
import torch.nn.functional as F
from HighwayBlock import *
import math

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

class NonLocalBlockND(nn.Module):
    """
    调用过程
    NONLocalBlock2D(in_channels=32),
    super(NONLocalBlock2D, self).__init__(in_channels,
            inter_channels=inter_channels,
            dimension=2, sub_sample=sub_sample,
            bn_layer=bn_layer)
    """
    def __init__(self,
                 in_channels,
                 inter_channels=None,
                 dimension=2,
                 bn_layer=True):
        super(NonLocalBlockND, self).__init__()

        assert dimension in [1, 2, 3]

        self.dimension = dimension

        self.in_channels = in_channels
        self.inter_channels = inter_channels

        if self.inter_channels is None:
            self.inter_channels = in_channels // 2
            # 进行压缩得到channel个数
            if self.inter_channels == 0:
                self.inter_channels = 1

        if dimension == 3:
            conv_nd = nn.Conv3d
            max_pool_layer = nn.MaxPool3d(kernel_size=(1, 2, 2))
            bn = nn.BatchNorm3d
        elif dimension == 2:
            conv_nd = nn.Conv2d
            max_pool_layer = nn.MaxPool2d(kernel_size=(2, 2))
            bn = nn.BatchNorm2d
        else:
            conv_nd = nn.Conv1d
            max_pool_layer = nn.MaxPool1d(kernel_size=(2))
            bn = nn.BatchNorm1d

        self.g = conv_nd(in_channels=self.in_channels,
                         out_channels=self.inter_channels,
                         kernel_size=1,
                         stride=1,
                         padding=0)

        if bn_layer:
            self.W = nn.Sequential(
                conv_nd(in_channels=self.inter_channels,
                        out_channels=self.in_channels,
                        kernel_size=1,
                        stride=1,
                        padding=0), bn(self.in_channels))
            nn.init.constant_(self.W[1].weight, 0)
            nn.init.constant_(self.W[1].bias, 0)
        else:
            self.W = conv_nd(in_channels=self.inter_channels,
                             out_channels=self.in_channels,
                             kernel_size=1,
                             stride=1,
                             padding=0)
            nn.init.constant_(self.W.weight, 0)
            nn.init.constant_(self.W.bias, 0)

        self.theta = conv_nd(in_channels=self.in_channels,
                             out_channels=self.inter_channels,
                             kernel_size=1,
                             stride=1,
                             padding=0)
        self.phi = conv_nd(in_channels=self.in_channels,
                           out_channels=self.inter_channels,
                           kernel_size=1,
                           stride=1,
                           padding=0)


    def forward(self, x):
        '''
        :param x: (b, c,  h, w)
        :return:
        '''

        batch_size = x.size(0)
        # print(x.shape) torch.Size([4, 64, 32, 32])
        #print( self.inter_channels) 32
        #print(self.g) Conv3d(64, 32, kernel_size=(1, 1, 1), stride=(1, 1, 1))

        
        g_x = self.g(x).view(batch_size, self.inter_channels, -1)#[bs, c, w*h]
        g_x = g_x.permute(0, 2, 1)

        theta_x = self.theta(x).view(batch_size, self.inter_channels, -1)
        theta_x = theta_x.permute(0, 2, 1)

        phi_x = self.phi(x).view(batch_size, self.inter_channels, -1)
        
        f = torch.matmul(theta_x, phi_x)

        #print(f.shape)

        f_div_C = F.softmax(f, dim=-1)

        y = torch.matmul(f_div_C, g_x)
        y = y.permute(0, 2, 1).contiguous()
        y = y.view(batch_size, self.inter_channels, *x.size()[2:])
        W_y = self.W(y)
        z = W_y + x
        return z


insertion = NonLocalBlockND

is_CELU = False
is_ELU = True


#SE模块
class SEModule(nn.Module):
    def __init__(self, channels, reduction=16):
        super(SEModule, self).__init__()
        self.avg_pool = nn.AdaptiveAvgPool2d(1)
        self.fc1 = nn.Conv2d(channels, channels // reduction, kernel_size=1, padding=0)
        if is_CELU :
            self.af = nn.CELU(inplace=True)
        elif is_ELU :
            self.af = nn.ELU(inplace=True)
        else :
            self.af = nn.ReLU(inplace=True)
        self.fc2 = nn.Conv2d(channels // reduction, channels, kernel_size=1, padding=0)
        self.sigmoid = nn.Sigmoid()

    def forward(self, input):
        x = self.avg_pool(input)
        x = self.fc1(x)
        #x = self.relu(x)
        x = self.af(x)
        x = self.fc2(x)
        x = self.sigmoid(x)
        return input * x


# 残差层
class Bottleneck(nn.Module):
    # 输出通道数为输入通道数的4倍
    expansion = 4

    def __init__(self, inplanes, planes, stride=1, downsample=None, scales=4, groups=1, is_first_block=0, se=True):
        super(Bottleneck, self).__init__()

        self.downsample = downsample
        self.scales = scales
        self.groups = groups
        self.stride = stride
        self.is_first_block = is_first_block

        outplanes = groups * planes
        # 第一个卷积层：卷积核尺寸： 1×1 ，填充值为 0， 步长为 1
        self.conv1 = nn.Conv2d(in_channels=inplanes, out_channels=outplanes, kernel_size=1, stride=1, bias=False)
        self.bn1 = nn.BatchNorm2d(outplanes)

        # 第二个卷积结构：卷积核尺寸： 3×3 ，填充值为 1， 步长为 1
        self.conv2 = nn.ModuleList([nn.Conv2d(outplanes // scales, outplanes // scales,
                                              kernel_size=3, stride=stride, padding=1, groups=groups, bias=False) for _ in
                                    range(scales - 1)])
        self.bn2 = nn.ModuleList([nn.BatchNorm2d(outplanes // scales) for _ in range(scales - 1)])

        # 第三个卷积层：卷积核尺寸： 1×1 ，填充值为 0， 步长为 1
        self.conv3 = nn.Conv2d(outplanes, planes * self.expansion, kernel_size=1, stride=1, bias=False)
        self.bn3 = nn.BatchNorm2d(planes * self.expansion)

        #self.relu = nn.ReLU(inplace=True)
        if is_CELU :
            self.af = nn.CELU(inplace=True)
        elif is_ELU :
            self.af = nn.ELU(inplace=True)
        else :
            self.af = nn.ReLU(inplace=True)
        
        # 处理第一个块
        if is_first_block == 1:
            self.pool = nn.AvgPool2d(kernel_size=3, stride=stride, padding=1)

        # SE模块
        self.se = SEModule(planes * self.expansion) if se else None
        self.insertion = insertion(inplanes)

    def forward(self, x):
        identity = x # 将原始输入暂存为shortcut的输出
        #x = self.insertion(x)
        # 对下采样进行处理
        if self.downsample is not None:
            identity = self.downsample(identity)

        # 1*1卷积层
        out = self.conv1(x)
        out = self.bn1(out)
        #out = self.relu(out)
        out = self.af(out)

        x_scales = torch.chunk(out, self.scales, 1)
        for i in range(self.scales-1):
            if i == 0 or self.is_first_block == 1:
                y_scale = x_scales[i]
            else:
                y_scale = y_scale + x_scales[i]
            y_scale = self.conv2[i](y_scale)
            #y_scale = self.relu(self.bn2[i](y_scale))
            y_scale = self.af(self.bn2[i](y_scale))
            if i == 0:
                out = y_scale
            else:
                out = torch.cat((out, y_scale), 1)
        if self.scales != 1 and self.is_first_block == 0:
            out = torch.cat((out, x_scales[self.scales-1]), 1)
        elif self.scales != 1 and self.is_first_block == 1:
            out = torch.cat((out, self.pool(x_scales[self.scales-1])), 1)

        # 1*1卷积层
        out = self.conv3(out)
        out = self.bn3(out)

        # 是否加入SE模块
        if self.se is not None:
            out = self.se(out)

        # 添加triplet_attention

        # 残差连接 out=F(X)+X
        out += identity
        #out = self.relu(out)
        out = self.af(out)

        return out


class Res2Net(nn.Module):
    def __init__(self, block, layers, num_classes=1000, scales=4, groups=1, se=True):
        super(Res2Net, self).__init__()
        # 通道数初始化
        self.inplanes = 64

        # 起始：7*7的卷积层，3*3的最大池化层
        self.conv1 = nn.Conv2d(3, self.inplanes, kernel_size=7, stride=2, padding=3, bias=False)
        self.bn1 = nn.BatchNorm2d(self.inplanes)
        #self.relu = nn.ReLU(inplace=True)
        if is_CELU :
            self.af = nn.CELU(inplace=True)
        elif is_ELU :
            self.af = nn.ELU(inplace=True)
        else :
            self.af = nn.ReLU(inplace=True)
        
        
        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)

        # 残差结构
        self.layer1 = self._make_layer(block, 64, layers[0], stride=1, scales=scales, groups=groups, se=se)
        self.layer2 = self._make_layer(block, 128, layers[1], stride=2, scales=scales, groups=groups, se=se)
        self.layer3 = self._make_layer(block, 256, layers[2], stride=2, scales=scales, groups=groups, se=se)
        self.layer4 = self._make_layer(block, 512, layers[3], stride=2, scales=scales, groups=groups, se=se)

        # 平均池化+全连接层
        self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
        self.fc = nn.Linear(512 * block.expansion, num_classes)

        # 权重初始化
        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
            elif isinstance(m, nn.BatchNorm2d):
                nn.init.constant_(m.weight, 1)
                nn.init.constant_(m.bias, 0)

    def _make_layer(self, block, planes, layer, stride=1, scales=4, groups=1, se=True):
        # 积步长不为1或深度扩张有变化，导致F(X)与X的shape不同的残差块，就要对X定义下采样函数，使之shape相同
        downsample = None
        if stride != 1 or self.inplanes != planes * block.expansion:
            downsample = nn.Sequential(
                nn.Conv2d(self.inplanes, planes * block.expansion, kernel_size=1, stride=stride, bias=False),
                nn.BatchNorm2d(planes * block.expansion),
            )

        layers = []
        # 第一个残差块需要下采样  def __init__(self, inplanes, planes, stride=1, downsample=None, scales=4, groups=1):
        layers.append(block(self.inplanes, planes, stride=stride, downsample=downsample,
                            scales=scales, groups=groups, is_first_block=1, se=se))
        self.inplanes = planes * block.expansion

        # 通过循环堆叠其余残差块
        for i in range(1, layer):
            layers.append(block(self.inplanes, planes, scales=scales, groups=groups, se=se))

        return nn.Sequential(*layers)

    def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        #x = self.relu(x)
        x = self.af(x)
        x = self.maxpool(x)

        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)

        x = self.avgpool(x)
        x = torch.flatten(x, 1)
        x = self.fc(x)

        return x


# def __init__(self, block, layers, num_classes=1000, scales=4, groups=1):
def res2net50(num_classes=100, scales=4, groups=1, se=True):
    return Res2Net(Bottleneck, [3, 4, 6, 3], num_classes, scales, groups, se)

def res2net18(num_classes=100, scales=4, groups=1, se=True):
    return Res2Net(Bottleneck, [2, 2, 2, 2], num_classes, scales, groups, se)

